//
//  LoginViewViewController.m
//  ai2ban
//
//  Created by Heartbeats on 2018/1/14.
//  Copyright © 2018年 mac book pro. All rights reserved.
//

#import "LoginViewViewController.h"
#import "LoginWithPadControl.h"
#import <SMS_SDK/SMSSDK.h>

@interface LoginViewViewController ()
@property (weak, nonatomic) IBOutlet UITextField *phoneTextFile;
@property (weak, nonatomic) IBOutlet UITextField *codeTextFile;
@property (weak, nonatomic) IBOutlet UITextField *psdTextFile;
@property (weak, nonatomic) IBOutlet UIButton *codeBtn;
@property (weak, nonatomic) IBOutlet UIButton *registBtn;

@end

@implementation LoginViewViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.registBtn.clipsToBounds = true;
    self.registBtn.layer.cornerRadius = 10;
    
    self.codeBtn.clipsToBounds = true;
    self.codeBtn.layer.cornerRadius = 8;
    self.codeBtn.layer.borderColor = [kWhiteColor CGColor];
    self.codeBtn.layer.borderWidth = 1;
    
}
- (void)viewWillAppear:(BOOL)animated {
    [self.navigationController.navigationBar setHidden:true];
}

- (IBAction)doBack:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:true];
}

- (IBAction)getCodeAction:(UIButton *)sender {
    [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:self.phoneTextFile.text zone:@"86"  result:^(NSError *error) {
        if (!error){
            [MozTopAlertView showWithType:0 text:@"请注意查收" parentView:self.view];
            UIColor *cColor = [UIColor lightGrayColor];
            [self setTheCountdownButton:sender startWithTime:60 title:@"获取验证码" countDownTitle:@"s" MainColor:mainColor cColor:mainColor];
            return;
        }
         [MozTopAlertView showWithType:MozAlertTypeError text:@"发送验证码失败" parentView:self.view];
    }];
}

- (IBAction)registOrLogin:(UIButton *)sender {
    if ([self.phoneTextFile.text isEqualToString:@""] || [self.codeTextFile.text isEqualToString:@""]) {
        [MozTopAlertView showWithType:0 text:@"请确认信息正确性" parentView:self.view];
        return;
    }
    [SMSSDK commitVerificationCode:self.codeTextFile.text phoneNumber:self.phoneTextFile.text zone:@"86" result:^(NSError *error) {
        if (!error) {
            NSDictionary *param = @{@"account": self.phoneTextFile.text, @"cpass": self.psdTextFile.text, @"phone": self.phoneTextFile.text, @"accountname": self.phoneTextFile.text, @"email": @""};
            [[NetworkManager shareNetwork] requestWithURLString:@"app/administrator/list/regist" parameters:param method:@"POST" callBack:^(id responseObject) {
                if ([responseObject[@"state"] intValue]) {
                    [AppManager loginWithUserID:responseObject[@"data"] psd:self.psdTextFile.text];
                    AppManager.isLogin = true;
                    [[NSNotificationCenter defaultCenter] postNotificationName:UpdateUserDataNotification object:nil];
                    [self dismissViewControllerAnimated:true completion:nil];
                    return;
                }
                [MozTopAlertView showWithType:MozAlertTypeError text:[NSString stringWithFormat:@"%@", responseObject[@"info"]] parentView:self.view];
            }];
            return;
        }
         [MozTopAlertView showWithType:MozAlertTypeError text:@"验证码错误" parentView:self.view];
    }];
}

- (void)setTheCountdownButton:(UIButton *)button startWithTime:(NSInteger)timeLine title:(NSString *)title countDownTitle:(NSString *)subTitle MainColor:(UIColor *)mColor cColor:(UIColor *)color {
    //倒计时时间
    __block NSInteger timeOut = timeLine;
    dispatch_queue_t queue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,0, 0, queue);
    //每秒执行一次
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL,0), 1.0 * NSEC_PER_SEC,0);
    dispatch_source_set_event_handler(_timer, ^{
        
        //倒计时结束，关闭
        if (timeOut == 0) {
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                button.backgroundColor = mColor;
                [button setTitle:title forState:UIControlStateNormal];
            
                button.userInteractionEnabled =YES;
            });
        } else {
            int seconds = timeOut % 60;
            NSString *timeStr = [NSString stringWithFormat:@"%0.1d", seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                button.backgroundColor = color;
                [button setTitle:[NSString stringWithFormat:@"%@%@",timeStr,subTitle]forState:UIControlStateNormal];
                button.userInteractionEnabled =NO;
            });
            timeOut--;
        }
    });
    dispatch_resume(_timer);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
